package nc.bs.med.sourcebill;

import java.util.Hashtable;

import javax.naming.NamingException;

import nc.bs.pub.SystemException;
import nc.bs.med.pub.smart.RichDMO;
import nc.itf.med.sourcebill.IMedSourceBill;
import nc.vo.pub.BusinessException;
import nc.vo.med.pub.MedEnv;
import nc.vo.med.sourcebill.MedSourceVO;
import nc.vo.med.sourcebill.MedSourcebillBVO;
import nc.vo.med.sourcebill.MedSourcebillVO;

public class MedSourceBillDMO extends RichDMO implements IMedSourceBill {

	public MedSourceBillDMO() throws NamingException, SystemException {
	}

	public MedSourceVO[] getAllSourceBillInfos() throws BusinessException {
		try {

			Hashtable<String, MedSourceVO> ht = new Hashtable<String, MedSourceVO>();
			String hsql = " select h.pk_billtype vbilltype,h.billno vbillcode,null vbilltypeitem,null vuiname,h.votable vmaintable, "
					+ " h.pkfield vmainkey,b.votable vftable,b.pkfield vmainkeyinftable, "
					+ "  h.operator voperator,h.operator vcuroperator,h.approveid vauditor,'pk_corp' vcorp, replace(t.forwardbilltype,',','/') vfwbilltype,"
					+ " t.nodecode vfuncode from pub_votable h,pub_votable b ,bd_billtype t where h.pk_billtype=b.pk_billtype and h.pk_billtype=t.pk_billtypecode "
					+ " and h.headbodyflag='Y' and b.headbodyflag='N' ";
			MedSourcebillVO[] hvos = (MedSourcebillVO[]) selectBySql(hsql,
					MedSourcebillVO.class);
			if (hvos == null || hvos.length == 0)
				return null;
			String ssql = " select pk_billtype vbilltype,def2 vsourcebilltype,def3 vsourcebill,1 pos,'Y' bdirsrc from pub_votable where headbodyflag='N'  ";
			MedSourcebillBVO[] bvos = (MedSourcebillBVO[]) selectBySql(ssql,
					MedSourcebillBVO.class);
			MedSourceVO avo = null;
			// do head
			for (int i = 0, iLen = hvos.length; i < iLen; i++) {
				avo = new MedSourceVO();
				avo.setParentVO(hvos[i]);
				ht.put(hvos[i].getPrimaryKey(), avo);
			}
			// do detail
			for (int i = 0, iLen = bvos.length; i < iLen; i++) {
				if (ht.containsKey(bvos[i].getVbilltype())) {
					avo = ht.get(bvos[i].getVbilltype());
					avo.setChildrenVO(addSourceVO((MedSourcebillBVO[]) avo
							.getChildrenVO(), bvos[i]));
				}
			}

			return ht.values().toArray(new MedSourceVO[ht.size()]);
		} catch (Exception e) {
			MedEnv.out(e);
			throw new BusinessException(e.getMessage());
		}
	}

	/**
	 * add extend sourceVOs
	 */
	private MedSourcebillBVO[] addSourceVO(MedSourcebillBVO[] bvos,
			MedSourcebillBVO bvo) {
		if (bvos == null || bvos.length == 0)
			return new MedSourcebillBVO[] { bvo };

		MedSourcebillBVO[] returnvos = new MedSourcebillBVO[bvos.length + 1];
		System.arraycopy(bvos, 0, returnvos, 0, bvos.length);
		returnvos[bvos.length] = bvo;
		return returnvos;

	}

	public MedSourceVO getSourceBillInfoByBillType(String sBillType)
			throws BusinessException {
		try {
			MedSourcebillVO hvo = (MedSourcebillVO) selectByKey(
					MedSourcebillVO.class, sBillType);
			MedSourcebillBVO[] bvos = (MedSourcebillBVO[]) selectBy(
					MedSourcebillBVO.class, null, "vbilltype='" + sBillType
							+ "'");
			MedSourceVO avo = new MedSourceVO();
			avo.setParentVO(hvo);
			avo.setChildrenVO(bvos);
			return avo;
		} catch (Exception e) {
			MedEnv.out(e);
			throw new BusinessException(e.getMessage());
		}

	}

}
